package tests;

import va.BitSet;
import va_file.VAFile;

public class VAFileTest {

	public static void main(String[] args) {
		testIntConvertValid();
		testIntConvertInvalid();
	}
	
	public static void testIntConvertValid() {
		try {
			BitSet correct = new BitSet(8);
			correct.set(0);
			correct.set(1);
			correct.set(2);
			correct.set(3);
			
			BitSet calculated = BitSet.FromInt(15, 8);
			assertBitSet(correct, calculated);
			
			correct = new BitSet(8);
			
			calculated = BitSet.FromInt(0, 4);
			assertBitSet(correct, calculated);
			
			correct = new BitSet(8);
			correct.set(3);
			
			calculated = BitSet.FromInt(8, 4);
			assertBitSet(correct, calculated);
			
			correct = new BitSet(8);
			correct.set(2);
			correct.set(3);
			
			calculated = BitSet.FromInt(12, 4);
			assertBitSet(correct, calculated);
			
			correct = new BitSet(8);
			correct.set(0);
			correct.set(1);
			
			calculated = BitSet.FromInt(3, 4);
			assertBitSet(correct, calculated);
			
			System.out.println("Passed testIntConvertValid");
		} catch (Exception e) {
			System.out.println("testIntConvertValid failed: " + e.getMessage());
		}	
	}
	
	public static void testIntConvertInvalid() {
		try {
			// too few bits
			BitSet calculated = BitSet.FromInt(3, 1);
			if (calculated != null) throw new Exception("3 has more than 1 bit");
			
			// negative number of bits
			calculated = BitSet.FromInt(3, -1);
			if (calculated != null) throw new Exception("negative number of bits");
			
			System.out.println("Passed testIntConvertInvalid");
		} catch (Exception e) {
			System.out.println("testIntConvertInvalid failed: " + e.getMessage());
		}	
	}
	
	public static void assertBitSet(BitSet correct, BitSet calculated) throws Exception {
		String correctString = correct.toString();
		String calculatedString = calculated.toString();
		if (!correctString.equals(calculatedString)) {
			throw new Exception("Correct bitset: " + correctString + " calculated bitset: " + calculatedString);
		}
	}

}
